package com.party.web.biz.gatherForm;

import com.google.common.collect.Lists;
import com.party.common.constant.Constant;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.gatherForm.GatherForm;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GatherFormInfoBizService {

    @Autowired
    private IMemberGroupService memberGroupService;

    /**
     * 导出Excel
     *
     * @param gatherForms
     * @param fields
     * @return
     */
    public SXSSFWorkbook exportExcel(List<Map<String, Object>> gatherForms, List<GatherForm> fields, String showResource) {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet0 = wb.createSheet();

        // 标题
        Row titleRow = sheet0.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("填写时间");
        for (int i = 0; i < fields.size(); i++) {
            Cell cell = titleRow.createCell(i + 1);
            cell.setCellValue(fields.get(i).getTitle());
        }

        // 内容
        for (int i = 0; i < gatherForms.size(); i++) {
            Row dataRow = sheet0.createRow(i + 1);

            Map<String, Object> map = gatherForms.get(i);
            String[] fieldValues = null != map.get("fieldValue")
                    ? map.get("fieldValue").toString().split("maxIndex") : new String[0];

            String[] fieldNames = null != map.get("fieldName")
                    ? map.get("fieldName").toString().split("maxIndex") : new String[0];

            Cell dateCell = dataRow.createCell(0);
            dateCell.setCellValue(DateUtils.formatDate((Date) map.get("createDate"), "yyyy-MM-dd HH:mm:ss"));

            boolean flag = false;
            for (int k = 0; k < fieldNames.length; k++) {
                if (fieldNames[k].equals(Constant.FIELD_RESOURCE_TITLE)) {
                    flag = true;
                }
            }

            if (StringUtils.isNotEmpty(showResource) && showResource.equals("1") && !flag) {
                // 需要显示来源，但是没有来源栏位
                List<String> newFieldNames = Lists.newArrayList();
                newFieldNames.add(Constant.FIELD_RESOURCE_TITLE);
                newFieldNames.add(Constant.FIELD_RESOURCELINK_TITLE);
                for (int j = 0; j < fieldNames.length; j++) {
                    newFieldNames.add(fieldNames[j]);
                }
                List<String> newFieldValues = Lists.newArrayList();
                newFieldValues.add("");
                newFieldValues.add("");
                for (int j = 0; j < fieldValues.length; j++) {
                    newFieldValues.add(fieldValues[j]);
                }

                for (int j = 0; j < newFieldValues.size(); j++) {
                    Cell dataCell = dataRow.createCell(j + 1);
                    if (newFieldNames.get(j).equals(Constant.FIELD_RESOURCE_TITLE)) {
                        MemberGroup member = memberGroupService.get(newFieldValues.get(j));
                        if (member != null) {
                            dataCell.setCellValue(member.getRealname());
                        } else {
                            dataCell.setCellValue("");
                        }
                    } else {
                        dataCell.setCellValue(newFieldValues.get(j));
                    }
                }
            } else {
                for (int j = 0; j < fieldValues.length; j++) {
                    Cell dataCell = dataRow.createCell(j + 1);
                    if (fieldNames[j].equals(Constant.FIELD_RESOURCE_TITLE)) {
                        MemberGroup member = memberGroupService.get(fieldValues[j]);
                        if (member != null) {
                            dataCell.setCellValue(member.getRealname());
                        } else {
                            dataCell.setCellValue("");
                        }
                    } else {
                        dataCell.setCellValue(fieldValues[j]);
                    }
                }
            }
        }
        return wb;
    }
}
